package main

import (
	"fmt"
	"os"
	codecs "sanguo/codec/cs"
	"sanguo/node/client/dispatcher"
	"sanguo/node/client/login"
	cs_msg "sanguo/protocol/cs/message"

	"github.com/golang/protobuf/proto"
	"github.com/sniperHW/kendynet"
)

func main() {

	if len(os.Args) < 3 {
		fmt.Printf("usage selectRole userID addr\n")
		return
	}

	userID := os.Args[1]
	addr := os.Args[2]

	login.Login(userID, addr, func(session kendynet.StreamSession, d *dispatcher.Dispatcher, msg *codecs.Message, err error) {
		if nil != err {
			fmt.Println(err)
		} else {
			gameLoginToC := msg.GetData().(*cs_msg.GameLoginToC)
			fmt.Println("RoleList", gameLoginToC)
			if gameLoginToC.GetCode() == cs_msg.EnumType_OK {
				var roleIDMap map[int]uint64 = make(map[int]uint64)

				req := &cs_msg.RoleListToS{}
				session.Send(codecs.NewMessage(uint16(0), req))

				d.Register(&cs_msg.RoleListToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleListToC)
					fmt.Println(resp.GetErrcode())
					for k, v := range resp.RoleList {
						fmt.Println(k, v)
						roleIDMap[k] = v.GetID()
						fmt.Println(k, roleIDMap[k])
					}

					d.Register(&cs_msg.RoleMissionToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleMissionToC)
						fmt.Println("RoleMissionToC ", resp)

					})

					d.Register(&cs_msg.RoleAttrToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleAttrToC)
						fmt.Println("RoleAttrToC ", resp)

					})

					d.Register(&cs_msg.RoleItemToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleItemToC)
						fmt.Println("RoleItemToC ", resp)

					})
					d.Register(&cs_msg.RoleBossZoneToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleBossZoneToC)
						fmt.Println("RoleBossZoneToC ", resp)

					})

					d.Register(&cs_msg.RoleWeaponToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleWeaponToC)
						fmt.Println("RoleWeaponToC ", resp)

					})

					d.Register(&cs_msg.CompleteMissionToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.CompleteMissionToC)
						fmt.Println("CompleteMission ", resp)

					})

					if resp.GetErrcode() == cs_msg.EnumType_OK {
						req2 := &cs_msg.SelectRoleToS{
							ID: proto.Uint64(roleIDMap[0]),
						}
						session.Send(codecs.NewMessage(uint16(0), req2))
						d.Register(&cs_msg.SelectRoleToC{}, false, func(args []interface{}) {
							resp2 := args[1].(*codecs.Message).GetData().(*cs_msg.SelectRoleToC)
							fmt.Println(resp2)
							if resp2.GetErrcode() == cs_msg.EnumType_OK {
								AcceptMission(session, resp2)
							}
						})
					}

				})

			}
		}
	})

	sigStop := make(chan bool)
	_, _ = <-sigStop
}

func AcceptMission(session kendynet.StreamSession, resp *cs_msg.SelectRoleToC) {
	var missionMap map[int]int32 = make(map[int]int32)
	go func() {
		for k, v := range resp.GetMissions() {
			missionMap[k] = v.GetId()
			fmt.Println(k, missionMap[k])
		}
		fmt.Printf("交付任务编号:")
		var id int
		fmt.Scan(&id)

		req := &cs_msg.CompleteMissionToS{
			MissionID: proto.Int32(missionMap[id]),
		}
		session.Send(codecs.NewMessage(uint16(0), req))

	}()

}
